package de.ars.client.items;

import java.util.Vector;

import de.ars.client.items.animators.CallPersonItemAnimator;
import de.ars.client.items.elements.ListItem;
import de.ars.client.paintSystems.StringPainter;
import de.ars.client.screens.MainScreen;

public class CallPersonItem extends AbstractSlideItem
{
	
	public CallPersonItem(int _width, int _height, MainScreen _parent, StringPainter _stringPainter)
	{
		super(_width,_height,_parent,_stringPainter);
	
    	this.animator = new CallPersonItemAnimator(this);
        this.animator.createNewAnimationPoint(this.layout.getAddPersonClosePos());
        this.animator.createNewAnimationPoint(this.layout.getAddPersonCloseSize());
        this.animator.createNewAnimationPoint(this.layout.getAddPersonCloseTextPos());
        this.animator.setLayout(this.layout);
        
        this.entryItems = new Vector();
	}
	
	private void callPerson()
	{
		String temp = this.parent.getSpecificPerson().getInformation("person.tel"+(this.selectedItemIndex+1));
		if (temp != "" && temp != null)
		{
			try {
				this.parent.getMidlet().platformRequest("tel:"+temp);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	public boolean keyPressed(int _key)
	{
		if (isOpen())
			if (!super.keyPressed(_key))
			{
				switch(_key)
		    	{
		    		case -6:
		    			this.animationType = ANIMATION_CLOSE;
		    			this.animator.closeItem();
		    			this.state = STATE_CLOSE_ANIMATION;
		    			return true;
		    		case -7:
		    			//Person anrufen
		    			this.callPerson();
		    			this.animationType = ANIMATION_CLOSE;
		    			this.animator.closeItem();
		    			this.state = STATE_CLOSE_ANIMATION;
		    			this.entryItems.removeAllElements();
		    			return true;
		    	}
			}
		return false;
	}
	
	public void open()
	{
		//open
		this.entryItems.removeAllElements();
		if (this.parent.getSpecificPerson().getInformation("person.tel1") != "" && this.parent.getSpecificPerson().getInformation("person.tel1") != null)
        	this.entryItems.addElement(new ListItem(this.stringPainter, this.parent.getSpecificPerson().getInformation("person.tel1"), "person.tel1"));
        if (this.parent.getSpecificPerson().getInformation("person.tel1") != "" && this.parent.getSpecificPerson().getInformation("person.tel2") != null)
        	this.entryItems.addElement(new ListItem(this.stringPainter, this.parent.getSpecificPerson().getInformation("person.tel2"), "person.tel2"));
        if (this.parent.getSpecificPerson().getInformation("person.tel1") != "" && this.parent.getSpecificPerson().getInformation("person.tel3") != null)
        	this.entryItems.addElement(new ListItem(this.stringPainter, this.parent.getSpecificPerson().getInformation("person.tel3"), "person.tel3"));
		super.open();
	}
}